package com.guoxin.part4.chapter21;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author guoxin
 * @date 2019/9/1
 */
public class ThreadLocalExample {
    public static void main(String[] args) {
        ThreadLocal<Integer> tl = new ThreadLocal<>();
        IntStream.range(0,10).forEach(i->{
            new Thread(()->{
                try {
                    tl.set(i);
                    System.out.println(Thread.currentThread()+" set i "+tl.get());
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(Thread.currentThread()+" get i "+tl.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        });
    }
}
